package com.lzz.bottomencapsulation.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by apple on 17/3/8.
 */

public class ViewPageAdapter extends PagerAdapter {
    private List<View> mListViews;

    public ViewPageAdapter(List<View> mListViews) {
        this.mListViews = mListViews;//构造方法，参数是我们的页卡，这样比较方便。
    }

    //直接继承PagerAdapter，至少必须重写下面的四个方法，否则会报错
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mListViews.get(position));//删除页卡
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mListViews.get(position).getParent() == null) {
            container.addView(mListViews.get(position));
        } else {
            ((ViewGroup) mListViews.get(position).getParent()).removeView(mListViews.get(position));
            container.addView(mListViews.get(position));
        }
        return mListViews.get(position);
    }

    @Override
    public int getCount() {
        return mListViews.size();//返回页卡的数量
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;//官方提示这样写
    }
}
